<?php

/**
 *  用户行为
 * @author xiaojy xiaojy@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2016, http://www.wanthings.com
 */
class UserInfoBehavior extends CActiveRecordBehavior
{

    /**
     * @param array $user 用户信息数组，电话，密码。
     * @return bool true false.
     *
     */
    public function getList($filter)
    {
        $result = array();
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('is_deleted' => 0));

        if (!empty($filter['sid'])) {
            $criteria->addCondition('sid=' . $filter['sid']);
        }

        if (!empty($filter['ids'])) {    //能够选择的ids
            $criteria->addInCondition('sid', $filter['ids']);
        }

        if (!empty($filter['min_time']) || !empty($filter['max_time'])) {
            if (!empty($filter['min_time']) && !empty($filter['max_time'])) {
                $criteria->addBetweenCondition('ctime', $filter['min_time'], $filter['max_time']);
            } elseif (!empty($filter['min_time']) && empty($filter['max_time'])) {
                $criteria->addCondition('ctime>' . $filter['min_time']);
            } else {
                $criteria->addCondition('ctime<=' . $filter['max_time']);
            }
        }

        if (isset($filter['style'])) {
            if ($filter['style'] == 1) {
                $criteria->order = 'id desc';
            } else {
                $criteria->order = 'id desc';
            }
        } else {
            $criteria->order = 'id desc';
        }

        if (!empty($filter['page']) || !empty($filter['page_size'])) {
            if (!empty($filter['page'])) {
                $page = $filter['page'];
            } else {
                $page = 1;
            }
            if (!empty($filter['page_size'])) {
                $page_size = $filter['page_size'];
            } else {
                $page_size = 20;
            }
            $count = $this->owner->model()->count($criteria);
            $pager = new CPagination($count);
            $pager->setItemCount($count);
            $pager->setCurrentPage($page - 1);
            $pager->setPageSize($page_size);
            $pager->validateCurrentPage = false;
            $pager->applyLimit($criteria);
            $result['pager'] = $pager;
        }
        $result['data'] = array();
        $query_result = $this->owner->findAll($criteria);
        if (!empty($query_result)) {
            foreach ($query_result as $key => $value) {
                $temp = $value->attributes;
                $result['data'][] = $temp;
            }
        }

        return $result;
    }


    private function findBySid($sid)
    {
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('sid' => $sid, 'is_deleted' => 0));
        $re = $this->owner->find($criteria);
        return $re;
    }


    private function findById($id)
    {
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('id' => $id, 'is_deleted' => 0));
        $re = $this->owner->find($criteria);
        return $re;
    }

    /**
     * @param $sid 用户sid
     * @return 用户信息数组.
     */
    public function getBySid($sid)
    {
        $result = array();
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('sid' => $sid, 'is_deleted' => 0));
        $re = $this->owner->model()->find($criteria);
        if (!empty($re)) {
            $temp = $re->attributes;
            $result['errno'] = 0;
            $result['errmsg'] = 'success';
            $result['result'] = $temp;
            return $result;
        } else {
            $result['errno'] = 3126;
            $result['errmsg'] = '用户不存在';
            return $result;
        }
    }

    //提供给api 
    public function getBySidToApi($sid)
    {
        $result = array();
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('sid' => $sid, 'is_deleted' => 0));
        $re = $this->owner->model()->find($criteria);
        if (!empty($re)) {
            $temp = $re->attributes;
            $user_info = array();
            $user_info['name'] = $temp['name'];
            $user_info['nickname'] = $temp['nickname'];
            $user_info['sid'] = $temp['sid'];
            $user_info['last_login_ip'] = $temp['last_login_ip'];
            $user_info['intro'] = $temp['intro'];
            if (!empty($temp['avatar'])) {
                $user_info['avatar'] = WxUtil::getGoodsImgUrl($temp['avatar']);
            } else {
                $user_info['avatar'] = Yii::app()->request->hostInfo . '/statics/image/def_avatar.png';
            }
            $user_info['ctime'] = $temp['ctime'];
            $user_info['email'] = $temp['email'];
            $user_info['phone'] = $temp['phone'];
            $user_info['gender'] = $temp['gender'];
            $result['errno'] = 0;
            $result['errmsg'] = 'success';
            $result['result'] = $user_info;
            return $result;
        } else {
            $result['errno'] = 3126;
            $result['errmsg'] = '用户不存在';
            return $result;
        }
    }

    public function getAvatar($sid)
    {
        $result = '';
        $user = $this->findBySid($sid);
        if (!empty($user->avatar)) {
            $result = WxUtil::getGoodsImgUrl($user->avatar);
        } else {
            $result = Yii::app()->request->hostInfo . '/statics/image/def_avatar.png';
        }
        return $result;
    }


    /**
     * @param $id 用户id
     * @return 用户信息数组.
     */
    public function getById($id)
    {
        $result = array();

        $re = $this->findById($id);
        if (!empty($re)) {
            $a = $re->attributes;
            $result['errno'] = 0;
            $result['errmsg'] = '';

            return $result;
        } else {
            $result['errno'] = 3126;
            $result['errmsg'] = '用户不存在';
            $result['result'] = '';
            return $result;
        }
    }

    //新增用户数据
    public function add($data)
    {
        $result = array();
        $result['errno'] = 0;
        $result['errmsg'] = 'success';
        $user_info = new UserInfo;

        if (empty($data['sid'])) {
            $result['errno'] = 1001;
            $result['errmsg'] = '请传用户id';
            return $result;
        }

        $user_info->sid = $data['sid'];

        if (!empty($data['name'])) {
            $user_info->name = $data['name'];
        }
        if (!empty($data['nickname'])) {
            $user_info->nickname = $data['nickname'];
        }
        if (!empty($data['last_login_ip'])) {
            $user_info->last_login_ip = $data['last_login_ip'];
        }
        if (!empty($data['intro'])) {
            $user_info->intro = $data['intro'];
        }
        if (!empty($data['avatar'])) {
            $user_info->avatar = $data['avatar'];
        }
        if (!empty($data['email'])) {
            $user_info->email = $data['email'];
        }
        if (!empty($data['phone'])) {
            $user_info->phone = $data['phone'];
        }
        if (!empty($data['gender'])) {
            $user_info->gender = $data['gender'];
        }
        $status = $user_info->save();
        if ($status) {
            $result['errno'] = 0;
            $result['errmsg'] = 'success';
        } else {
            $result['errno'] = 1;
            $result['errmsg'] = '保存失败';
        }
        return $result;
    }

    //更新用户数据
    public function updateUserInfo($data)
    {
        $result = array();
        $result['errno'] = 0;
        $result['errmsg'] = 'success';

        if (empty($data['sid'])) {
            $result['errno'] = 1001;
            $result['errmsg'] = '请传用户id';
            return $result;
        }

        $user_info = $this->findBySid($data['sid']);

        if (empty($user_info)) {
            $result['errno'] = 1002;
            $result['errmsg'] = '用户信息不存在';
            return $result;
        }

        if (!empty($data['name'])) {
            $user_info->name = $data['name'];
        }
        if (!empty($data['nickname'])) {
            $user_info->nickname = $data['nickname'];
        }
        if (!empty($data['last_login_ip'])) {
            $user_info->last_login_ip = $data['last_login_ip'];
        }
        if (!empty($data['intro'])) {
            $user_info->intro = $data['intro'];
        }
        if (!empty($data['avatar'])) {
            $user_info->avatar = $data['avatar'];
        }
        if (!empty($data['email'])) {
            $user_info->email = $data['email'];
        }
        if (!empty($data['last_login_ip'])) {
            $user_info->last_login_ip = $data['last_login_ip'];
        }
        if (!empty($data['phone'])) {
            $user_info->phone = $data['phone'];
        }
        if (!empty($data['gender'])) {
            $user_info->gender = $data['gender'];
        }
        $status = $user_info->save();
        if ($status) {
            $result['errno'] = 0;
            $result['errmsg'] = 'success';
        } else {
            $result['errno'] = 1;
            $result['errmsg'] = '保存失败';
        }
        return $result;
    }


    //获取用户信息 强制使用缓存
    public function getCacheInfo($sid = 0)
    {
        $result = array();
        $cache_key = 'cache_data_user_info_id_' . $sid;
        $cache_data = Yii::app()->cache->get($cache_key);
        if (!empty($cache_data)) {
            return $cache_data;
        }
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('is_deleted' => 0));
        $criteria->addCondition('sid = ' . $sid);
        $data = $this->getOwner()->find($criteria);
        if (!empty($data)) {
            $result = $data->attributes;
            $result['avatar'] = WxUtil::getgoodsimgurl($result['avatar']);
        }
        Yii::app()->cache->set($cache_key, $result);
        return $result;
    }


}

